package ulric.kit;

import java.io.IOException;
import java.net.DatagramSocket;
import java.net.ServerSocket;

/**
 * @author ulric.qin@gmail.com
 */
public class SystemKit {

    public static boolean portInUse(int port) {
        ServerSocket ss = null;
        DatagramSocket ds = null;
        try {
            ss = new ServerSocket(port);
            ss.setReuseAddress(true);
            ds = new DatagramSocket(port);
            ds.setReuseAddress(true);
            return false;
        } catch (IOException e) {
        } finally {
            if (ds != null) {
                ds.close();
            }

            if (ss != null) {
                try {
                    ss.close();
                } catch (IOException e) {
                    // should not be thrown, just detect port available.
                }
            }
        }
        return true;
    }

}
